package com.isoftstone.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.isoftstone.entity.Department;
import com.isoftstone.entity.Doctor;
import com.isoftstone.entity.Patient;
import com.isoftstone.entity.Register;
import com.isoftstone.entity.Role;
import com.isoftstone.entity.Users;
import com.isoftstone.service.PatientService;
import com.isoftstone.service.UserService;

@Controller
@RequestMapping("opreg")
public class PatientController {

	@Autowired
	private PatientService patientService;
	
	@RequestMapping("loadOptRegistPage")
	public String loadOptRegistPage(Model model){
		//查询科室信息
		List<Department> list=patientService.selectDepartments();
		model.addAttribute("departments",list);
		return "opreg/optRegist";
		
	}
	
	
	
	@RequestMapping("patientList")
	public String getAllPatient(ModelMap modelMap){
	// TODO Auto-generated method stub
		
		List<Patient> patientList=patientService.getAllPatientList();
		
		/*System.out.println(patientList);*/

		modelMap.addAttribute("patientList",patientList);
		
		
		
		return "PatientList";
		
	}
	
	
	
	/*添加病人*/
	@RequestMapping("insertPatient")
	@ResponseBody
	public int insertPatient(String patient_name,int patient_sex,int patient_age,Date patient_born,String patient_address,
			String patient_idcard,String patient_tel,String patient_country,String patient_race,String patient_edu,String patient_job,
			String patient_xuexing,int patient_hunyin,String patient_idaddress){

			Patient patient=new Patient();
			patient.setCard_id(UUID.randomUUID().toString());
			patient.setPatient_name(patient_name);
			patient.setPatient_sex(patient_sex);
			patient.setPatient_age(patient_age);
			patient.setPatient_born(patient_born);
			patient.setPatient_address(patient_address);
			patient.setPatient_idcard(patient_idcard);
			patient.setPatient_tel(patient_tel);
			patient.setPatient_country(patient_country);
			patient.setPatient_race(patient_race);
			patient.setPatient_edu(patient_edu);
			patient.setPatient_job(patient_job);
			patient.setPatient_xuexing(patient_xuexing);
			patient.setPatient_hunyin(patient_hunyin);
			patient.setPatient_idaddress(patient_idaddress);
			
				
			int flag = patientService.insertPatient(patient);
			return flag;	
	}
	
	//通过名字或身份证号查询病人信息列表
	@RequestMapping("selectPatients")
	public String  selectPatients(String regName,String regCode,Model model){
		System.out.println(regName+":"+regCode);
		Patient entity=new Patient();
		if(regName!=null&&!regName.equals("")){
			entity.setPatient_name(regName);
		}
		if(regCode!=null&&!regCode.equals("")){
			entity.setPatient_idcard(regCode);
			
		}
		
		List<Patient> list=patientService.selectPatientByName(entity);
		model.addAttribute("rows", list);//页面根据rows这个key获取数据即可
		return "opreg/optPatients";
	}
	
	//按照身份证查询病人信息
	@RequestMapping("selectPatientById")
	/*@ResponseBody*/
	public String getPatientBypatient_idcard(String patient_idcard,Model model){
		
		List<Department> list=patientService.selectDepartments();
		model.addAttribute("departments",list);
		
		Patient patient = patientService.getPatientBypatient_idcard(patient_idcard);
		model.addAttribute("entity", patient);
		return "opreg/optRegist";

	}

	//按照科室id查询医生列表
	@RequestMapping("selectDoctors")
	@ResponseBody
	public List<Doctor> selectHcareworkers(String dept_id){
		//查询医生列表
		//System.out.println("-------"+deptId);
		List<Doctor> list=patientService.selectDoctors(dept_id);
		return list;
	}
	
	//判断该病人是否存在
	@RequestMapping("selectPatientInfoExists")
	@ResponseBody
	public Boolean selectPatientInfoExists(String idnumber,String name){
		Patient e=new Patient();
		e.setCard_id(idnumber);
		e.setPatient_name(name);
	List<Patient> list=	patientService.selectPatientByName(e);
		if(list!=null&&list.size()>0){
			return true;
		}
		
		return false;
	}
	
	//保存挂号信息
	@RequestMapping("addRegister")
	public String addRegister(String idnumber,int dept_id){
		System.out.println(  idnumber+":"+":"+dept_id);
		Register e=new Register();
		e.setCard_id(idnumber);
		e.setDept_id(dept_id);
		e.setGuahao_id(UUID.randomUUID().toString());
		e.setZhenduan_type(0);
	    patientService.insertRegister(e);

		return "opreg/success";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		
	//转换日期
	DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
	}

}
